package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.ScrollPane;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import DB.DBUtil;
import IO.MyIoper;
import Thread.DrawThread;
import Thread.SaveThread;

import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.DefaultComboBoxModel;
import javab.EnTypeBalance;
import javax.swing.JTextField;
import javax.swing.JButton;
import javab.EnTypeSearch;
import javab.Logs;
import javab.User;

import javax.swing.JScrollPane;
import javax.swing.JTable;

import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Vector;
import java.awt.event.ActionEvent;

public class MyJFrame extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTable table;
	DBUtil dbu=new DBUtil();
	MyIoper myi=new MyIoper();
	ArrayList<Logs> arlistlog=new ArrayList<Logs>();
	ArrayList<User> arlistusers=new ArrayList<User>();
	private JTextField textField_1;
	private int select=0;
	Connection con;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MyJFrame frame = new MyJFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MyJFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 604, 499);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu menu = new JMenu("菜单");
		menuBar.add(menu);
		
		JMenuItem menuItem = new JMenuItem("数据库连接");
		
		//数据库连接
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				con=dbu.getDBConnection("mysql");
				if(con!=null) {
					JOptionPane.showMessageDialog(null, "数据库连接成功");
				}else {
					JOptionPane.showMessageDialog(null, "数据库连接错误");
				}
			}
		});
		
		
		menu.add(menuItem);
		
		JMenuItem menuItem_1 = new JMenuItem("数据库断开");
		
		//数据库断开
		menuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(con==null) {
					JOptionPane.showMessageDialog(null, "未连接数据库");
				}else {
					dbu.closeAll();
					con=null;
					JOptionPane.showMessageDialog(null, "数据库已断开");
					
				}
				
			}
		});
		
		
		menu.add(menuItem_1);
		
		JMenuItem menuItem_2 = new JMenuItem("文件另存为");
		
		
		menu.add(menuItem_2);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("请选择：");
		label.setBounds(10, 35, 54, 15);
		contentPane.add(label);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(EnTypeBalance.values()));
		comboBox.setBounds(79, 32, 60, 21);
		contentPane.add(comboBox);
		
		textField = new JTextField();
		textField.setBounds(181, 32, 66, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JButton button = new JButton("确认");
		
		button.setBounds(282, 31, 93, 23);
		contentPane.add(button);
		
		JLabel label_1 = new JLabel("查找记录：");
		label_1.setBounds(10, 89, 60, 15);
		contentPane.add(label_1);
		
		JComboBox comboBox_1 = new JComboBox();
		comboBox_1.setModel(new DefaultComboBoxModel(EnTypeSearch.values()));
		comboBox_1.setBounds(80, 86, 60, 21);
		contentPane.add(comboBox_1);
		
		JButton button_1 = new JButton("确认");
		
		
		
		
		
		button_1.setBounds(282, 85, 93, 23);
		contentPane.add(button_1);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 139, 568, 258);
		contentPane.add(scrollPane);
		

		
		textField_1 = new JTextField();
		textField_1.setBounds(181, 86, 66, 21);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		//Jtable
		table=new JTable();
		scrollPane.setViewportView(table);
		
	
		
		
		//查询操作
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String input=textField_1.getText().trim();
				if(con==null) {
					JOptionPane.showMessageDialog(null, "请先连接数据库");
				}
				else {

					switch((EnTypeSearch)comboBox_1.getSelectedItem()) {
					case 取款:
						
						arlistlog.clear();
						arlistlog=dbu.queryByOpert("取款");
						break;
					case 存款:
						
						arlistlog.clear();
						arlistlog=dbu.queryByOpert("存款");
						break;
					case 时间:
						
						arlistlog.clear();
						if(input.length()==0) {
							arlistlog=dbu.queryById("2018001");
						}
						else {
							arlistlog=dbu.queryByTime(input);
						}
						break;
					case 金额:
						
						arlistlog.clear();
						if(input.length()==0) {
							arlistlog=dbu.queryById("2018001");
						}else {
							arlistlog=dbu.queryByBalance(input);
						}
						break;
					case 账号:
						
						arlistlog.clear();
						arlistlog=dbu.queryById("2018001");
						break;
					}
					
					if(arlistlog.size()==0) {
						JOptionPane.showMessageDialog(null, "找不到相关记录");
					}
					else {
						Vector<String> v=new Vector<String>();
						v.add("ID");
						v.add("Opera");
						v.add("Num");
						v.add("Time");
						DefaultTableModel df=new DefaultTableModel(v,0);
						for(Logs l:arlistlog) {
							String[] line= {l.getId(),l.getOperating(),String.valueOf(l.getBalance()),l.getTime()};
							df.addRow(line);
						}
						table.setModel(df);
					}
				}
//				
				
			}
		});
		
		
		
		//存钱取钱
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String id="2018001";
				String input=textField.getText().trim();
				if(con==null) {
					JOptionPane.showMessageDialog(null, "请先连接数据库");
				}else {
					if(input.length()==0) {
						JOptionPane.showMessageDialog(null, "请输入金额");
					}else {
						int num=Integer.valueOf(input);
						arlistusers=dbu.queryById1(id);
						switch((EnTypeBalance)comboBox.getSelectedItem()) {
						case 取钱:
							if(arlistusers.get(0).getBalance()<num) {
								JOptionPane.showMessageDialog(null, "余额不足");
							}else {
								User u=arlistusers.get(0);
								System.out.println(u.getId());
								DrawThread dt=new DrawThread(u, num);
								dt.start();
								try {
									dt.sleep(100);
								} catch (InterruptedException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
								
							}
							
							break;
						case 存钱:
							User u=arlistusers.get(0);
							SaveThread st=new SaveThread(u, num);
							st.start();
							System.out.println("存钱");
							try {
								st.sleep(100);
							} catch (InterruptedException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							break;
						}
						
						
						
						
						arlistlog.clear();
						arlistlog=dbu.queryById("2018001");
						
						System.out.println(arlistlog.size());
						Vector<String> v=new Vector<String>();
						v.add("ID");
						v.add("Opera");
						v.add("Num");
						v.add("Time");
						DefaultTableModel df=new DefaultTableModel(v,0);
						for(Logs l:arlistlog) {
							String[] line= {l.getId(),l.getOperating(),String.valueOf(l.getBalance()),l.getTime()};
							df.addRow(line);
						}
						table.setModel(df);
					}
				}
				
			}
		});
		
		//文件序列化保存
		menuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(con==null) {
					JOptionPane.showMessageDialog(null, "未连接数据库");
				}else {
					JFileChooser jf=new JFileChooser();
					if(jf.showSaveDialog(MyJFrame.this)==JFileChooser.APPROVE_OPTION) {
						String filename=jf.getSelectedFile().getAbsolutePath();
						if(arlistlog.size()==0) {
							arlistlog=dbu.queryById("2018001");
							myi.serSave(filename,arlistlog);
						}else {
							myi.serSave(filename,arlistlog);
						}
						
					}
				}
				
			}
		});		
	}
}
